package controller;

import model.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IManagerService;
import service.impl.ManagerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Autowired
    private IManagerService managerService;


    @RequestMapping("/loginPage")
    public String loginPage(){
        return  "manager/login";
    }


    @RequestMapping("/login")
    @ResponseBody
    public String login(Manager manager, HttpServletRequest request){
         Manager m = managerService.login(manager);
        if(m == null){
            return "0";
        }
        HttpSession session = request.getSession();
        session.setAttribute(m.getId().toString(),m);
        session.setMaxInactiveInterval(300);
        return  m.getId().toString();
    }


    @RequestMapping("/indexPage")
    public ModelAndView indexPage(String id, HttpServletRequest request){
        HttpSession session = request.getSession();
        //根据id从session中获取管理员对象
        Manager m = (Manager) session.getAttribute(id);

        ModelAndView mv = new ModelAndView();
        mv.addObject("mName", m.getmName());
        mv.addObject("mLevel", m.getmLevel());
        mv.setViewName("manager/index");
        return  mv;
    }

    @RequestMapping("/list")
    public ModelAndView list(Manager manager){
        return  managerService.totalCount(manager);
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "manager/admin_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(Manager manager){
       return String.valueOf(managerService.add(manager));
    }

    @RequestMapping("/on")
    @ResponseBody
    public String on(Manager manager){
        return  String.valueOf(managerService.on(manager));
    }

    @RequestMapping("/off")
    @ResponseBody
    public String off(Manager manager) {
        return  String.valueOf(managerService.off(manager));
    }


}
